package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentMyFollowBinding;
import com.ja.openchat.viewmodels.MyFollowPagerGroupViewModel;
import com.ja.openchat.viewmodels.MyFollowViewModel;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

//我的关注和粉丝
public class MyFollowFragment extends BaseFragment<FragmentMyFollowBinding, MyFollowViewModel> {

    public static MyFollowFragment newInstance(@IntRange(from = 0, to = 2) int type, long userId) {
        MyFollowFragment fragment = new MyFollowFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putLong("userId", userId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_follow;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initView() {
        binding.refreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            int type = arguments.getInt("type");
            viewModel.setType(type);
            viewModel.setData(arguments);
        }
        MyFollowPagerGroupViewModel myFollowPagerGroupViewModel = ViewModelProviders.of(getActivity()).get(MyFollowPagerGroupViewModel.class);
        viewModel.setVm(myFollowPagerGroupViewModel);
//        myFollowPagerGroupViewModel.searchAction.observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(String s) {
//                KLog.d("onChanged==" + s);
//                KLog.d("onChanged type==" + arguments.getInt("type"));
//                viewModel.doSearch();
//            }
//        });
        viewModel.setActivity(getActivity());
        binding.refreshLayout.autoRefresh();
    }

    public void doSearch() {
        viewModel.doSearch();
    }

    @Override
    public void initViewObservable() {
        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(this, o -> {
            //结束刷新
            binding.refreshLayout.finishRefresh();
        });
        //监听上拉加载完成
        viewModel.uc.finishLoadmore.observe(this, o -> {
            //结束刷新
            binding.refreshLayout.finishLoadMore();
        });
        //监听是否启用加载更多
        viewModel.uc.enableFinishLoadmore.observe(this, aBoolean -> {
            KLog.d("enableFinishLoadmore==" + aBoolean);
            binding.refreshLayout.setEnableLoadMore(aBoolean);
        });
    }
}
